package com.lx.ngsl.core.login.controller;

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lx.ngsl.core.dto.Rap;
import com.lx.ngsl.core.dto.UserInfo;
import com.lx.ngsl.core.login.service.LoginAuth;
import com.lx.ngsl.core.sso.service.TokenService;
import com.lx.ngsl.core.user.entity.UserEntity;
import com.lx.ngsl.core.user.service.UserService;
import com.lx.ngsl.util.UuidUtil;

/**
 * 登录控制器类
 * @ClassName:：LoginController 
 */
@RestController
@RequestMapping("/v0")
public class LoginController {
	@Autowired
	private LoginAuth loginAuth;
	@Autowired
	private UserService userService;
	@Autowired
	private TokenService tokenService;
	
	@Value("${server.servlet.context-path}")
	private String contextPath;
	
	@Value("${app.login-path}")
	private String loginPath;
	
	/**
	 * 用户登录
	 * @Title：login 
	 * @param username
	 * @param password
	 * @param session
	 * @param response
	 * @param request
	 * @return 
	 * @return ：Map<String,Object> 
	 * @throws LoginException 
	 * @throws
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Map<String, Object> login(
			@RequestParam(value="username",required = true, defaultValue = "") String username,
			@RequestParam(value="password",required = true, defaultValue = "") String password,
			HttpSession session, HttpServletResponse response,HttpServletRequest request) throws LoginException{
		Map<String,Object> map = new HashMap<>();
		UserEntity loginUser = new UserEntity();
		loginUser.setUsername(username);
		loginUser.setPassword(password);
		//登录验证
		UserEntity user;
		try {
			user = loginAuth.loginAuth(loginUser);
		} catch (Exception e) {
			//e.printStackTrace();
			return Rap.error(e.getMessage());
		}
		
		//用户登录成功时生成一个唯一的token令牌
		String privatetoken = UuidUtil.id();
		
		UserInfo userInfo = userService.toUserInfo(user);
		//userInfo.setMwmm(password);//保存明文密码到模型对象中
		userInfo.setPrivatetoken(privatetoken);//保存私有令牌到模型对象中
		
		//将此用户的登录信息存储到服务器的session中。
		session.setAttribute("USER_CONTEXT", userInfo);
		
		//保存token信息
//		TokenEntity token =  new TokenEntity();
//		token.setToken(privatetoken);//保存token令牌 
//		token.setUserInfo(userInfo);//保存用户登录信息信息 
		
		//将令牌保存至reids缓存库中
//		tokenService.saveToken(token);
//		map.put("token", token);
		return Rap.success();
	}

	/**
	 * 用户注销
	 * @Title：logout 
	 * @param session
	 * @param response
	 * @param request
	 * @throws Exception 
	 * @return ：void 
	 * @throws
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public void logout(HttpSession session, HttpServletResponse response,HttpServletRequest request) throws Exception {
		UserInfo userInfo = (UserInfo)session.getAttribute("USER_CONTEXT"); //session中取出当前用户信息
	
		if (userInfo != null) {
			//根据当前用户的令牌删除 Reids中的 token对象即可，如果Redis中没有这个token对象，则请求无效。
//			TokenEntity tokenRecord = tokenService.getToken(userInfo.getPrivatetoken());//从redis中获取token对象
//			tokenService.delTocken(tokenRecord);//从redis中删除token对象
//			
//			tokenRecord = tokenService.getToken(userInfo.getPrivatetoken());
//			
//			if(tokenRecord != null){
//				System.out.println("Reids中的令牌对象未清除，注销失败！！！");
//			}
			
			//将此用户的登录信息从服务器的session中清空。
			session.setAttribute("USER_CONTEXT", null);
		}
		
		response.sendRedirect(contextPath + loginPath);//返回到登录页面
	}

}
